layui.use(['table', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#peisongTime'
        	,value:new Date()
    });

    /**
     * 托盘表管理
     */
    var BasicTray = {
        tableId: "basicTrayTable"
    };
     var HxStorageCenter = {
        tableId: "hxStorageCenterTable"
    };


    /**
     * 初始化表格的列
     */
    HxStorageCenter.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'stockPort', sort: true, title: '备货口'
            },
            {field: 'getUnit', sort: true, title: '门店'},
            {field:'操作',title:'操作',toolbar:'#tableBar'}
        ]];
    };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageCenter.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxStorageCenter/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxStorageCenter.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    // 渲染表格
    var tableDataResult1 = table.render({
        elem: '#' + HxStorageCenter.tableId,
        url: Feng.ctxPath + '/hxStorageCenter/list',
        page: true,
        limit: 120,
        limits:[120,240,360,480,1500,5000],
        height: "full-60",
        cellMinWidth: 100,
        cols: HxStorageCenter.initColumn()
    });
//添加按钮
    $("#addButton").click(function () {
        var val = $("#rangerid").val();
        console.log(val);
        $.ajax({
            url:  Feng.ctxPath + "/hxStorageCenter/insertstockPort?stockports="+$("#rangerid").val(),
            type: 'POST',
            data: {
            },
            success: function(response){
                console.log(response);
                table.reload('hxStorageCenterTable');
            },
            error: function(xhr, status, error){
                console.error(error);
            }
        });
        table.reload('hxStorageCenterTable');
    });



    /**
     * 初始化表格的列
     */
    BasicTray.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'getUnitId', hide: true, title: '主键'},
            {field: 'getUnit', sort: true, title: '门店',
            	templet: function(d){
            		var str = "";
            		if (d.stockPort != null && d.stockPort != '') {
						str += '<span style="color: #43CF7C;">'+d.getUnit+'</span>';
					} else {
						str = d.getUnit
					}
            		return str
            	}
			},
            {field: 'outVolume', sort: true, title: '体积m³',
            	templet: function(d){
            		var str = "";
            		if (d.stockPort != null && d.stockPort != '') {
						str += '<span style="color: #43CF7C;">'+d.outVolume+'</span>';
					} else {
						str = d.outVolume
					}
            		return str
            	}
			},
            {field: 'singleQuantity', sort: true, title: '单量',
            	templet: function(d){
            		var str = "";
            		if (d.stockPort != null && d.stockPort != '') {
						str += '<span style="color: #43CF7C;">'+d.singleQuantity+'</span>';
					} else {
						str = d.singleQuantity
					}
            		return str
            	}
			},
            {field: 'stockPort', sort: true, title: '备货口',
            	templet: function(d){
            		var str = "";
            		if (d.stockPort != null && d.stockPort != '') {
						str += '<span style="color: #43CF7C;">'+d.stockPort+'</span>';
					} else {
						str = d.stockPort
					}
            		return str
            	}
			},
            {field: 'fpstockPort', title: '<span style="color: #2b85e4;"><i class="layui-icon">&#xe716;</i> 分配，多个用英文,隔开</span>', style:'color: #2b85e4;', edit:'text'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicTray.search = function () {
        var queryData = {};
        queryData['peisongTime'] = $("#peisongTime").val();
        queryData['condition'] = $("#condition").val();
        table.reload(BasicTray.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicTray.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicTray.exportExcel();
    });
    // 监听全部清空按钮点击事件
    $('#clear-all').on('click', function(){

        var ajax = new $ax(Feng.ctxPath + "/hxStorageCenter/clearall"  ,function (data) {
            if (data.success) {
                table.reload(HxStorageCenter.tableId);
                Feng.success("清空成功!");
            } else {
                Feng.error("清空失败！"+data.message);
            }
        }, function (data) {
            Feng.error("清空失败!" + data.responseJSON.message + "!");
        });
        ajax.start();

        // 重新渲染表格
        table.reload('hxStorageCenterTable');

    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicTray.tableId,
        url: Feng.ctxPath + '/hxStorageCenter/findHxStorageOutFenpeiBySpec',
        page: true,
        limit: 120,
		limits:[120,240,360,480,1500,5000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicTray.initColumn()
    });


    // 工具条点击事件
    table.on('tool(' + HxStorageCenter.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            // HxStorageCenter.openEditDlg(data);
            var ajax = new $ax(Feng.ctxPath + "/hxStorageCenter/editItem?id=" + data.id ,function (data) {
                if (data.success) {
                    table.reload(HxStorageCenter.tableId);
                    Feng.success("清空成功!");

                } else {
                    Feng.error("清空失败！"+data.message);
                }
            }, function (data) {
                Feng.error("清空失败!" + data.responseJSON.message + "!");
            });
            ajax.start()
        } else if (layEvent === 'delete') {
            HxStorageCenter.onDeleteItem(data);
        }
    });
    // 工具条点击事件
    table.on('edit(' + BasicTray.tableId + ')', function (obj) {
        var data = obj.data;
		var value = obj.value;//修改后
        var field = obj.field;
		if(field == 'fpstockPort'){
	        var ajax = new $ax(Feng.ctxPath + "/hxStorageCenter/fpstockPort?getUnitId="+data.getUnitId+"&getUnit="+data.getUnit+"&stockPorts="+value+"&peisongTime="+$("#peisongTime").val(), function (data) {
	    		if (data.success) {
                	table.reload(BasicTray.tableId);
	                Feng.success("分配成功!");
                    table.reload('hxStorageCenterTable');
                } else {
	                Feng.error("分配失败！"+data.message);
				}
	        }, function (data) {
	            Feng.error("分配失败!" + data.responseJSON.message + "!");
	        });
	        ajax.start();
		}
    });
});
